class Solution {
    public int maxProfit(int[] prices) {
        int maxNum = prices[prices.length-1]; //最大数字
        int maxProfit = 0;      //最大盈利金额
        //边界检查
        if (prices.length == 1){
            return 0;
        }
        //倒序遍历数组
        for (int i = prices.length-1; i >= 0; i--) {
            if (i == prices.length-1){
                continue;   //直接沿用最开头定义的maxNum和maxProfit即可
            }
            // 最大盈利金额就是：maxNum - prices[i]
            if (maxNum - prices[i] > maxProfit){    //选出新的最大盈利金额
                maxProfit = maxNum - prices[i];
            }
            if (prices[i] > maxNum){    //选出新的最大数字
                maxNum = prices[i];
            }
        }
        return maxProfit;
    }
}